﻿
using HBase;
using HBase.cache;
using hCommand.dto;
using hCommand.entity;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Text;
using System.Web;

namespace hCommand {
    [RoutePrefix("api/equp")]
    public class EqupServe : BaseApiController {


        IChouYangServer chouYangServer = WMSDalFactray.getDal<IChouYangServer>();


        [HttpGet, Route("my/{openid}")]
        public List<CyEqup> my(string openid) {


            CyUser u = MyCache.getValue<CyUser>("cy_User", openid);
            if (u == null) { 
            return new List<CyEqup> { };
            }
            return u.getEqup();

        }
        [HttpPost, Route("FindPage")]
        public BaseResult FindPage([FromBody] EqupDto dto) {
            Page<CyEqup> page= MyCache.page<CyEqup>(dto, (a) => {
                if (String.IsNullOrEmpty(dto.custom)) {
                    return true;
                }
                CyEqup e = ((CyEqup)a);
                if (String.IsNullOrEmpty(e.custom)) {
                    return false;
                }
                return e.custom.IndexOf(dto.custom) != -1;
            });
            return BaseResult.Ok(page);
        }

        [HttpGet, Route("addUserEqup/{openId}/{equCode}")]
        public long addUserEqup(string openId, string equCode) {
            CyUser u = MyCache.getValue<CyUser>("cy_User", openId);
            if (u == null) {
                return -1;
            }

            CyEqup old = MyCache.getValue<CyEqup>("CyEqup", equCode);
            if (old.userId != 0) {
                return -2;
            }
            old.userId = u.id;
           
            chouYangServer.update("", old);
            TaskPool.doWork<WcsLoadUp>();
            return 0;
        }

        [HttpGet, Route("updataEqupAlias/{id}/{alias}")]
        public long updataEqupAlias(string id, string alias) {
            long change = DateTime.Now.Ticks;
            alias = HttpUtility.UrlDecode(alias);

            CyEqup old = MyCache.getValue<CyEqup>("CyEqup", id);
            old.alias = alias;
            chouYangServer.update(BaseConfig.BaseDBConnStr,old);
            TaskPool.doWork<WcsLoadUp>();
            return 0;
        }
        [HttpGet, Route("delete/{id}")]
        public long delete(string id) {
            long change = DateTime.Now.Ticks;
            CyEqup old = MyCache.getValue<CyEqup>("CyEqup", id);
            old.userId = 0;
            chouYangServer.update("", old);
            TaskPool.doWork<WcsLoadUp>();
            return 1;
        }
        [HttpPost, Route("save")]
        public long addEqup(CyEqup equp) {
       
         
            chouYangServer.update(BaseConfig.BaseDBConnStr, equp);
            TaskPool.doWork<WcsLoadUp>();

            return equp.id;

        }


        [HttpGet, Route("his/{openId}/{equId}/{size}")]
        public Page<EqupUse> his(string openId, string equId, int size) {
            CyUser u = MyCache.getValue<CyUser>("User", openId);
            if (u == null) {
                return new Page<EqupUse>();
            }
            string suffix = u.suffix;
            string[] suffArr = suffix.Split('@');
            PageDto dto = new PageDto() { pageSize = 10, pageNum = size };
            Page<EqupUse> page = chouYangServer.queryPage<EqupUse>(suffArr[1], "select * from equp_use" + suffArr[0] + " where equp_id='" + equId + "'", " id ", dto);
            return page;

        }

    }
}
